package com.alipay.app.groupmeal.standard.http;

import static com.alipay.app.groupmeal.standard.constant.ConfigConstant.tenantId;

import com.alibaba.fastjson.JSONObject;
import com.alipay.app.groupmeal.standard.bean.AliPayResVo;
import com.alipay.app.groupmeal.standard.bean.FoodBean;
import com.alipay.app.groupmeal.standard.http.bean.resp.AliOrderDTO;
import com.alipay.app.groupmeal.standard.http.bean.resp.BaseResp;
import com.alipay.app.groupmeal.standard.http.bean.resp.HexiaoOrderModel;
import com.alipay.app.groupmeal.standard.http.bean.resp.QrPayModel;
import com.alipay.app.groupmeal.standard.http.bean.resp.QuerySignResp;
import com.alipay.app.groupmeal.standard.http.bean.resp.Result;
import com.alipay.app.groupmeal.standard.http.bean.resp.WithholdResp;
import com.alipay.app.groupmeal.standard.http.bean.resp.WithholdValidateResp;
import com.alipay.app.groupmeal.standard.offline.bean.FkBean;
import com.alipay.app.groupmeal.standard.offline.bean.SbruanDinnerBean;
import com.alipay.app.groupmeal.standard.util.Totp;
import com.alipay.iot.sdk.utils.SystemUtils;
import com.alipay.sdk.util.cloudsaleapi.bean.QuerySignBizContentReq;
import com.alipay.sdk.util.cloudsaleapi.bean.WithholdReq;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.rxjava3.core.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class RetrofitHelper {

    private ApiService mApiService;
    private static RetrofitHelper helper;

    public static RetrofitHelper getInstance() {
        if (helper == null) {
            synchronized (RetrofitHelper.class) {
                if (helper == null) {
                    helper = new RetrofitHelper();
                }
            }
        }
        return helper;
    }

    private synchronized ApiService getApiService() {
        if (mApiService == null) {
            mApiService = ApiEngine.getInstance().getApiService();
        }
        return mApiService;
    }
//
//    /**
//     * 代扣
//     *
//     * @param bizContent
//     * @return
//     */
//    public Observable<BaseResp<WithholdResp>> withhold(WithholdReq.BizContent bizContent,
//                                                       String appId, String merchantAppId) {
//        Map<String, Object> params = new HashMap<>();
//        params.put("app_id", appId);
//        params.put("merchant_app_id", merchantAppId);
//        params.put("method", "cloudsale_api_pay");
//        params.put("biz_content", JSONObject.toJSONString(bizContent));
//        return getApiService().withhold(params);
//    }
//
//    /**
//     * 代扣校验
//     *
//     * @param bizContent
//     * @return
//     */
//    public Observable<BaseResp<WithholdValidateResp>> withholdValidate(WithholdReq.BizContent bizContent,
//                                                                       String appId, String merchantAppId) {
//        Map<String, Object> params = new HashMap<>();
//        params.put("app_id", appId);
//        params.put("merchant_app_id", merchantAppId);
//        params.put("method", "cloudsale_api_pay_validate");
//        params.put("biz_content", JSONObject.toJSONString(bizContent));
//        return getApiService().withholdValidate(params);
//    }
//
//    /**
//     * 查询用户签约协议API
//     *
//     * @param bizContent
//     * @return
//     */
//    public Observable<BaseResp<QuerySignResp>> querySign(QuerySignBizContentReq bizContent,
//                                                         String appId, String merchantAppId) {
//        Map<String, Object> params = new HashMap<>();
//        params.put("app_id", appId);
//        params.put("merchant_app_id", merchantAppId);
//        params.put("method", "cloudsale_api_sign_query");
//        params.put("biz_content", JSONObject.toJSONString(bizContent));
//        return getApiService().querySign(params);
//    }


    public Observable<Result<AliPayResVo>> cardPay(String price,
                                                   String cardsn, String tradeNo,
                                                   String sessionId) {
        Map<String, Object> params = new HashMap<>();
        params.put("deviceId", SystemUtils.getSerialNumber());
        params.put("price", price);
        params.put("cardsn", cardsn);
        params.put("tradeNo", tradeNo);
        params.put("sessionId", sessionId);
        return getApiService().cardPay(params, tenantId, Totp.getTOTP(tenantId));
    }


    public Observable<Result<AliPayResVo>> cardPayV2(String price,
                                                     String cardsn, String tradeNo,
                                                     String sessionId, int onLine, List<FoodBean> foodBeanList, Date createTime) {
        JSONObject params = new JSONObject();
        params.put("deviceId", SystemUtils.getSerialNumber());
        params.put("price", price);
        params.put("auth", cardsn);
        params.put("tradeNo", tradeNo);
        params.put("sessionId", sessionId);
        params.put("orders", tradeNo);
        params.put("onLine", onLine);
        params.put("foodBeanList", foodBeanList);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        params.put("dateStr", sdf.format(createTime));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), params.toJSONString());
        return getApiService().cardPayV2(requestBody, tenantId, Totp.getTOTP(tenantId));
    }

    public Observable<Result<AliPayResVo>> qrPayV2(String price,
                                                   String auth,
                                                   String orders,
                                                   String terminalParams,
                                                   List<FoodBean> foodBeanList) {
        JSONObject params = new JSONObject();
        params.put("deviceId", SystemUtils.getSerialNumber());
        params.put("price", price);
        params.put("auth", auth);
        params.put("tradeNo", orders);
        params.put("sessionId", orders);
        params.put("orders", orders);
        params.put("terminal_params", terminalParams);
        params.put("foodBeanList", foodBeanList);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), params.toJSONString());
        return getApiService().qrPayV2(requestBody, tenantId, Totp.getTOTP(tenantId));
    }

    public Observable<Result<AliPayResVo>> facePayV2(String price, String faceToken,
                                                     String aliUid,
                                                     String tradeNo, String sessionId,
                                                     String terminalParams,
                                                     List<FoodBean> foodBeanList) {
        JSONObject params = new JSONObject();
        params.put("deviceId", SystemUtils.getSerialNumber());
        params.put("price", price);
        params.put("tradeNo", tradeNo);
        params.put("sessionId", sessionId);
        params.put("faceToken", faceToken);
        params.put("aliUid", aliUid);
        params.put("terminal_params", terminalParams);
        params.put("foodBeanList", foodBeanList);
        return getApiService().facePayV2(params, tenantId, Totp.getTOTP(tenantId));
    }

    public Observable<Result<AliPayResVo>> facePay(String price, String faceToken,
                                                   String aliUid,
                                                   String tradeNo, String sessionId,
                                                   String terminalParams) {
        Map<String, Object> params = new HashMap<>();
        params.put("deviceId", SystemUtils.getSerialNumber());
        params.put("price", price);
        params.put("tradeNo", tradeNo);
        params.put("sessionId", sessionId);
        params.put("faceToken", faceToken);
        params.put("aliUid", aliUid);
        params.put("terminal_params", terminalParams);
        return getApiService().facePay(params, tenantId, Totp.getTOTP(tenantId));
    }

    public Observable<Result<JSONObject>> checkOrder(String deviceOrders) {
        return getApiService().checkOrder(deviceOrders, tenantId, Totp.getTOTP(tenantId));
    }

    public Observable<Result<String>> deviceSign() {
        return getApiService().deviceSign(SystemUtils.getSerialNumber(), tenantId, Totp.getTOTP(tenantId));
    }

    public Observable<Result<FkBean>> readCard(String cardSn) {
        return getApiService().readCard(cardSn, tenantId, Totp.getTOTP(tenantId));
    }

    public Observable<Result<JSONObject>> getCensus() {
        return getApiService().getCensus(SystemUtils.getSerialNumber(), tenantId, Totp.getTOTP(tenantId));
    }


    public Observable<Result<List<AliOrderDTO>>> getOder() {
        return getApiService().getOder(SystemUtils.getSerialNumber(), tenantId, Totp.getTOTP(tenantId));
    }

    public Observable<Result<List<SbruanDinnerBean>>> downMainpSbruanDinner() {
        return getApiService().downMainpSbruanDinner(tenantId, Totp.getTOTP(tenantId));
    }

    public Observable<Result<String>> weixinPay(String authCode, String price) {
        return getApiService().weixinPay(authCode, price,tenantId, Totp.getTOTP(tenantId));
    }

    public Observable<Result<List<FkBean>>> downFk(String times) {
        return getApiService().downFk(times,tenantId, Totp.getTOTP(tenantId));
    }


    public Observable<Result<String>> upLoadOrder(String amount, String tradeNo,
                                                  Date createTime,
                                                  String authCode, String cardno) {
        Map<String, Object> params = new HashMap<>();
        params.put("deviceId", SystemUtils.getSerialNumber());
        params.put("amount", amount);
        params.put("authCode", authCode);
        params.put("cardno", cardno);
        params.put("tradeNo", tradeNo);
        params.put("createTime", createTime.getTime());
        return getApiService().upLoadOrder(params, tenantId, Totp.getTOTP(tenantId));
    }

    public Observable<Result<QrPayModel>> toQrPay(String price, String authCode, String order) {
        Map<String, Object> params = new HashMap<>();
        params.put("deviceId", SystemUtils.getSerialNumber());
        params.put("price", price);
        params.put("authCode", authCode);
        params.put("order", order);
        return getApiService().toQrPay(params, tenantId, Totp.getTOTP(tenantId));
    }

    public Observable<Result<QrPayModel>> checkQrOrders(String order) {
        Map<String, Object> params = new HashMap<>();
        params.put("order", order);
        return getApiService().checkQrOrders(params, tenantId, Totp.getTOTP(tenantId));
    }

    /**
     * 刷卡核销请求，文化局使用中，请注意修改
     *
     * @param cardSn
     * @return
     */
    public Observable<Result<HexiaoOrderModel>> cardHexiao(String cardSn) {
        Map<String, Object> params = new HashMap<>();
        params.put("deviceId", SystemUtils.getSerialNumber());
        params.put("cardSn", cardSn);
        return getApiService().cardHexiao(params, tenantId, Totp.getTOTP(tenantId));
    }


    /**
     * 刷卡核销请求，文化局使用中，请注意修改
     *
     * @param authCode
     * @param faceToken
     * @param aliUid
     * @param tradeNo
     * @param sessionId
     * @param terminalParams
     * @return
     */
    public Observable<Result<AliPayResVo>> hexiao1(String authCode, String faceToken,
                                                   String aliUid,
                                                   String tradeNo, String sessionId, String terminalParams) {
        Map<String, Object> params = new HashMap<>();
        params.put("deviceId", SystemUtils.getSerialNumber());
        params.put("tradeNo", tradeNo);
        params.put("sessionId", sessionId);
        params.put("authCode", authCode);
        params.put("faceToken", faceToken);
        params.put("aliUid", aliUid);
        params.put("terminal_params", terminalParams);
        return getApiService().hexiao1(params, tenantId, Totp.getTOTP(tenantId));
    }
}
